'use strict';

const Controller = require('egg').Controller;

class LookController extends Controller {
    //获取
    async getLook() {
        const { ctx } = this;
        const { page } = ctx.query;
        let data = await this.app.mysql.select('Look');
        let arr = data.slice((page - 1) * 10, page * 10);
        ctx.body = {
            code: 200,
            data,
            arr,
        };
    }

    //搜索
    async searchLook() {
        const { ctx } = this;
        const { name, state, page } = ctx.query;
        console.log(page);
        let data = await this.app.mysql.query(`SELECT * FROM look WHERE name Like "%${name}%" 
        and state Like "%${state}%"`);
        let arr = data.slice((page - 1) * 10, page * 10);
        ctx.body = {
            code: 200,
            data,
            arr
        };
    }

    //删除
    async deleteLook() {
        const { ctx } = this;
        const { id } = ctx.query;
        const data = await this.app.mysql.delete('look', {
            id: id,
        });
        if (data.affectedRows !== 1) {
            ctx.body = {
                code: 500,
                message: "删除失败"
            };
        }
        ctx.body = {
            code: 200,
            message: "删除成功"
        };
    }

    //开关的编辑
    async stateLook() {
        const { ctx } = this;
        const { id } = ctx.request.body;
        let list = await this.app.mysql.get('look', { id: id });
        list.type = !list.type;
        const data = await this.app.mysql.update('look', {
            ...list
        });
        if (data.affectedRows !== 1) {
            ctx.body = {
                code: 500,
                message: "修改失败"
            };
        }
        ctx.body = {
            code: 200,
            message: "修改成功"
        };
    }
}

module.exports = LookController;
